package com.ajk.shop.dao;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.ajk.shop.dao.provider.GoodsImageProvider;
import com.ajk.shop.dao.provider.GoodsMapperProvider;
import com.ajk.shop.model.Category;
import com.ajk.shop.model.CommentLabel;
import com.ajk.shop.model.Goods;
import com.ajk.shop.model.GoodsCategory;
import com.ajk.shop.model.GoodsImage;
import com.ajk.shop.model.GoodsSortConfig;
import com.ajk.shop.model.PostageConfig;
import com.ajk.shop.model.SystemCity;
import com.ajk.shop.model.request.GoodsCommentReq;
import com.ajk.shop.model.request.GoodsReq;
import com.ajk.shop.model.response.CategoryRes;
import com.ajk.shop.model.response.GoodsRes;
import com.ajk.shop.model.response.MarketingActivityRes;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2015年12月30日 下午1:27:05 
 * 类说明 
 */
public interface GoodsMapper {
	
	@Update("update goods set amount = amount - #{num} where id = #{goodsId}")
	public void subtractStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	
	@Update("update goods set amount = amount + #{num} where id = #{goodsId}")
	public void increaseStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	
	@Update("update goods set buy_count = (select buy_count + b.goods_number from order_info a,order_detail b where a.id = b.order_id and a.order_no "
			+ " = #{orderNo} and b.is_service != 1 and goods.id = b.goods_id ) where "
			+ " (select 1 from order_info a,order_detail b where a.id = b.order_id and a.order_no = #{orderNo} "
			+ " and b.is_service != 1 and goods.id = b.goods_id )")
	public Integer updateGoodsBuyCountByOrderNo(String orderNo);
	
	/**
	 * 评价商品
	 * @param orderCommentReq
	 * @return
	 */
	@Insert("insert into shop_comment(order_id,goods_id,user_id,is_manager,comment,goods_source,"
			+ " shipping_source,help_source,avg_source,is_open) values "
			+ " (#{orderId},#{goodsId},#{userId},#{isManager},#{comment},#{goodsSource},"
			+ " #{shippingSource},#{helpSource},#{avgSource},#{isOpen})")
	@Options(useGeneratedKeys=true)
	public Integer addShopComment(GoodsCommentReq goodsCommentReq);
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsHomeList")
	public List<GoodsRes> getHomeList(GoodsReq goodsReq);
	
	@Select("select * from goods where id = #{id}")
	public GoodsRes getGoodsDetailById(Integer id);
	
	@Select("select * from goods_image where goods_id = #{goodsId} and status = 1")
	public List<GoodsImage> getGoodsImageList(Integer goodsId);
	
	@Select("select category_id id from goods_category where goods_id = #{goodsId}")
	public List<Category> getGoodsCategoryByGoodsId(Integer goodsId);
	
	@Select("select * from t_system_city")
	public List<SystemCity> getAllRegion();
	
	/**
	 * 根据省ID获取邮费配置信息
	 * @param provinceId
	 * @return
	 */
	@Select("select * from postage_config where province_id = #{provinceId}")
	public PostageConfig getPostageConfigByProvinceId(Integer provinceId);
	
	/**
	 * 只用大于100的ID，小于100的用于自定义
	 * @return
	 */
	@Select("select * from category where status = 1 and parent_id = 0 order by sort_order desc ")
	public List<CategoryRes> categoryList();
	
	@Select("select * from category where status = 1 and parent_id = #{parentId} order by sort_order desc ")
	public List<CategoryRes> categoryListByParentId(Integer parentId);
	
	@Select("select id,name,seq from goods_sort_config order by seq desc")
	public List<GoodsSortConfig> sortList();
	
	@Select("select id,name,seq,config from goods_sort_config where id = #{id}")
	public GoodsSortConfig getGoodsSortConfig(Integer id);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsList")
	public List<GoodsRes> goodsList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsListCount")
	public Integer goodsListCount(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="medtronicGoodsList")
	public List<GoodsRes> medtronicGoodsList(GoodsReq goodsReq);
	
	@Select("select * from marketing_activity where id = #{id}")
	public MarketingActivityRes queryMarketingActivityById(Integer id);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="marketingActivityGoodsList")
	public List<GoodsRes> marketingActivityGoodsList(GoodsReq goodsReq);
	/**
	 * 评价标签列表
	 * @return
	 */
	@Select("select id,name from comment_label where in_used = 1")
	public List<CommentLabel> commentLabelList();
	
	@Select("select id,title,image,display_col from category where recommend = 1  order by sort_order desc")
	public List<CategoryRes> homeCategoryList();
	
	@Select("select id,title,image from category where recommend = 2 order by sort_order desc")
	public List<CategoryRes> homeBannerList();
	
	@Select("select a.* from goods a left join goods_category b on a.id = b.goods_id where a.is_onsale = 1 and b.category_id  = #{categoryId}")
	public List<GoodsRes> findGoodsByCategoryId(GoodsReq goodsReq);
	/**
	 * 管理平台商品列表
	 * @param goodsReq
	 * @return
	 */
	@SelectProvider(type=GoodsMapperProvider.class,method="managerList")
	public List<GoodsRes> managerList(GoodsReq goodsReq);
	/**
	 * 添加分类
	 * @param category
	 * @return
	 */
	@Insert("insert into category(title,level,parent_id,display_col) values(#{title},#{level},#{parentId},#{displayCol})")
	public Integer insertCategory(Category category);
	/**
	 * 更新分类
	 * @param category
	 */
	@UpdateProvider(type=GoodsMapperProvider.class,method="updateCatetory")
	public void updateCategory(Category category);
	/**
	 * 根据标签查询分类
	 * @param title
	 * @return
	 */
	@Select("select id,title,image from category where title = #{title}")
	public CategoryRes getCategoryByTitle(String title);
	
	@Select("select id,title,image from category where title = #{title} and id != #{id}")
	public CategoryRes getCategoryByTitleAndId(@Param("title") String title,@Param("id") Integer id);
	/**
	 * 删除分类
	 * @param id
	 * @return
	 */
	@Delete("delete from category where id = #{id}")
	public Integer deleteCategory(Integer id);
	/**
	 * 设置分类在首页展示
	 * @param category
	 * @return
	 */
	@Update("update category set recommend = #{recommend} where id = #{id}")
	public Integer categoryHomeView(Category category);
	
	/**
	 * 设置分类在banner位展示
	 * @param category
	 * @return
	 */
	@Update("update category set recommend = #{recommend},image = #{image} where id = #{id}")
	public Integer categoryBannerView(Category category);
	
	/**
 	* 新增商品 
 	*/
 	@InsertProvider(method="insert",type=GoodsMapperProvider.class)
	@Options(useGeneratedKeys=true)
 	public Integer insertGoods(Goods domain);
 
 	
 	/**
 	*更新商品 
 	*/
 	@UpdateProvider(method="update",type=GoodsMapperProvider.class)
 	public Integer updateGoods(Goods domain);
 	
 	/**
 	* 新增商品图片
 	*/
 	@InsertProvider(method="insert",type=GoodsImageProvider.class)
	@Options(useGeneratedKeys=true)
 	public Integer insertGoodsImage(GoodsImage domain);
 
 	
 	/**
 	*更新商品图片 
 	*/
 	@UpdateProvider(method="update",type=GoodsImageProvider.class)
 	public Integer updateGoodsImage(GoodsImage domain);
 	
 	
 	/**
 	 * 获取当前最大的sort_order排序值
 	 */
 	@Select("select max(sort_order) from goods")
 	public Integer maxSortOrder();
 	
 	/**
 	* 新增商品图片
 	*/
 	@InsertProvider(method="insertGoodsCategory",type=GoodsMapperProvider.class)
	@Options(useGeneratedKeys=true)
 	public Integer insertGoodsCategory(GoodsCategory domain);
 	
 	@Delete("delete from goods_category where id = #{id}")
 	public Integer deleteGoodsCategoryById(Integer id);
 	
 	@Select("select * from goods_category where goods_id = #{goodsId}")
 	public List<GoodsCategory> findGoodsCategoryByGoodsId(Integer goodsId);
}